package com.htgd.gardenexpo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConTelescopeMeal;
import com.htgd.gardenexpo.page.PageBase;
import com.htgd.gardenexpo.service.GeConTelescopeMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: 王秀田
 * @date: 2024/11/15 15:43
 * @description: 望眼镜套餐管理
 */
@RestController
@RequestMapping("/api/telescopeMeal")
@Api(tags = "望眼镜套餐管理")
public class GeConTelescopeMealController {

    @Autowired
    private GeConTelescopeMealService geConTelescopeMealService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    @Transactional
    public CommonResult<JSONObject> add(@RequestBody GeConTelescopeMeal geConTelescopeMeal) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConTelescopeMeal.setCreateTime(LocalDateTime.now());
            geConTelescopeMeal.setStatus("0");
            geConTelescopeMealService.save(geConTelescopeMeal);
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    @Transactional
    public CommonResult<JSONObject> edit(@RequestBody GeConTelescopeMeal geConTelescopeMeal) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConTelescopeMeal.setUpdateTime(LocalDateTime.now());
            geConTelescopeMealService.updateById(geConTelescopeMeal);
            result.setCode(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    @Transactional
    public CommonResult<JSONObject> delete(String id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeConTelescopeMeal geConTelescopeMeal = new GeConTelescopeMeal();
            geConTelescopeMeal.setDeleteTime(LocalDateTime.now());
            geConTelescopeMeal.setStatus("1");
            geConTelescopeMeal.setId(id);
            geConTelescopeMealService.updateById(geConTelescopeMeal);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConTelescopeMeal>> list(PageBase pageBase) {
        CommonResult<List<GeConTelescopeMeal>> result = new CommonResult<>();
        try {
            PageHelper.startPage(pageBase.getPageNum(), pageBase.getPageSize());
            List<GeConTelescopeMeal> list = geConTelescopeMealService.list(
                    new LambdaQueryWrapper<GeConTelescopeMeal>()
                            .eq(GeConTelescopeMeal::getStatus,"0")
                            .orderByAsc(GeConTelescopeMeal::getPrice));
            PageInfo<GeConTelescopeMeal> data = new PageInfo<>(list);
            result.setCode(200);
            result.setMessage(String.valueOf(data.getTotal()));
            result.setData(data.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
